﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBH.Model.Controller;
using QLBH.DataModel;
using QLBH.StandardUI;
using QLBH.ModelInterface;

namespace QLBH.SaleProduct
{
    public partial class FindPNKDlg : Form
    {
        private struct sFindInfo
        {
            public String sophieu { set; get; }
            public Guid khoid { set; get; }
            public Guid nvid { set; get; }
            public DateTime fromdate { set; get; }
            public DateTime todate { set; get; }            
        }

        private sFindInfo finInfo;
        private IPhieuNhapKhoModel model;

        public IPhieuNhapKhoModel Model
        {
            get { return model; }
            set { model = value; }
        }
        public FindPNKDlg()
        {
            InitializeComponent();

            finInfo = new sFindInfo();
        }

        private void FindPNKDlg_Load(object sender, EventArgs e)
        {
            //Load List kho
            List<KHO> khos = KhoController.GetInstance().GetAll().ToList();

            cmbKho.DataSource = khos;

            cmbKho.DisplayMember = "TENKHO";

            KHO defaultKho = khos.Where(k => k.MACDINH == true).FirstOrDefault();
            if (defaultKho != null)
            {
                cmbKho.SelectedItem = defaultKho;
            }

            //Load list nhan vien
            List<NHANVIEN> users = NhanVienController.GetInstance().GetAll().ToList();
            cmbUser.DataSource = users;
            cmbUser.DisplayMember = "TENNV";

            Guid nvid= AccountHandler.GetCurrentUserLogin().NVID;
            NHANVIEN loginUser = NhanVienController.GetInstance().FindBy(nv => nv.NVID ==nvid ).FirstOrDefault();

            if (loginUser!=null)
            {
                cmbUser.SelectedItem = loginUser;
            }
            // setting for date time to now.

            dtpFromdate.Value = DateTime.Now;
            dtpToDate.Value = DateTime.Now;   
        }

        private void tbxSoPhieu_TextChanged(object sender, EventArgs e)
        {
            finInfo.sophieu = tbxSoPhieu.Text;            
        }

        private void cmbKho_SelectedIndexChanged(object sender, EventArgs e)
        {
            finInfo.khoid = (cmbKho.SelectedItem as KHO).KHOID;            
        }

        private void dtpFromdate_ValueChanged(object sender, EventArgs e)
        {
            finInfo.fromdate = dtpFromdate.Value;            
        }

        private void dtpToDate_ValueChanged(object sender, EventArgs e)
        {
            finInfo.todate = dtpToDate.Value;        
        }

        private void cmbUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            finInfo.nvid = (cmbUser.SelectedItem as NHANVIEN).NVID;            
        }

        private void FindResult()
        {
            List<IPhieuNhapKhoModel> result = new List<IPhieuNhapKhoModel>();
            List<PHIEUNHAPKHO> pnks = 
                PhieuNhapKhoController.GetInstance().FindBy(
                    pn => pn.SOPHIEU == finInfo.sophieu ||
                    pn.KHOID==finInfo.khoid ||
                    pn.NGUOITAOID==finInfo.nvid ||
                    (pn.NGAYTAO>=finInfo.fromdate && 
                    pn.NGAYTAO<=finInfo.todate)
                ).ToList();
            foreach (PHIEUNHAPKHO item in pnks)
            {
                IPhieuNhapKhoModel modelItem = ModelFactory.ModelFactory.GetPhieuNhapKhoModelInstance();
                modelItem.InitFromEntity(item);
                result.Add(modelItem);
            }
            phieuNhapKhoModelBindingSource.DataSource = new BindingList<IPhieuNhapKhoModel>(result);

            dataGridViewEx1.DataSource = phieuNhapKhoModelBindingSource;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            FindResult();
        }

        private void dataGridViewEx1_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridViewEx1.SelectedRows.Count>0)
            {
                btnEdit.Enabled = true;
                btnDelete.Enabled = true;
                model = dataGridViewEx1.SelectedRows[0].DataBoundItem as IPhieuNhapKhoModel;
            }
            else
            {
                btnEdit.Enabled = false;
                btnDelete.Enabled = false;
            }
        }

        private void FindPNKDlg_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode==Keys.Enter)
            {
                btnSearch_Click(btnSearch, null);
            }
            else if (e.KeyCode==Keys.Delete)
            {
                btnDelete_Click(btnDelete, null);
            } 
            else if (e.KeyCode==Keys.F1)
            {
                tbxSoPhieu.SelectAll();
                tbxSoPhieu.Focus();
            }
            else if (e.KeyCode == Keys.F2)
            {
                btnEdit_Click(btnEdit, null);
            }
            else if (e.KeyCode==Keys.Escape)
            {
                this.Close();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            // send edit object to parent form and close this dialog
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            //show question to assume that user want to delete
            if (ShowMessage.Instance.ShowQuestion("Bạn muốn xóa phiếu nhập kho này?") == DialogResult.Yes)
            {
                try
                {
                    PhieuNhapKhoController.GetInstance().Delete(model.ToEntity());
                    FindResult();
                }
                catch (System.Exception ex)
                {
                    ShowMessage.Instance.ShowError(ex.Message);
                }                
            }
            return;
        }

        private void dataGridViewEx1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (dataGridViewEx1.SelectedRows.Count > 0)
            {
                model = dataGridViewEx1.SelectedRows[0].DataBoundItem as IPhieuNhapKhoModel;
            }
            btnEdit_Click(btnEdit, null);
        }

        private void dataGridViewEx1_DataSourceChanged(object sender, EventArgs e)
        {
            if (dataGridViewEx1.Rows.Count>0)
            {
                //btnEdit
            }
        }
    }
}
